package cn.itsource.hrm.service.impl;

import cn.itsource.hrm.domain.Employee;
import cn.itsource.hrm.domain.Tenant;
import cn.itsource.hrm.dto.TenantDto;
import cn.itsource.hrm.exception.ErrorCode;
import cn.itsource.hrm.exception.GloabException;
import cn.itsource.hrm.mapper.EmployeeMapper;
import cn.itsource.hrm.mapper.TenantMapper;
import cn.itsource.hrm.service.ITenantService;
import cn.itsource.hrm.util.ValidityUtils;
import cn.itsource.hrm.util.encrypt.MD5;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.xml.crypto.Data;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gwkge
 * @since 2021-03-31
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {
    
    @Autowired
    private EmployeeMapper employeeMapper;

    @Value("${hrm.tenant.mealtime}")
    private int mealTime;
    
    
    /**
     * 机构入驻
     * 1. 从tenantDto 中获取机构，员工，套餐 数据
     * 2. 判断数据的合法性（非空，重复，格式验证） // 一定不要相信前端
     * 3. 保存机构数据
     * 4. 保存员工数据
     * 5. 保存机构和套餐的关系
     * 
     * @param tenantDto
     */
    @Override
    public void entering(TenantDto tenantDto) {
        
        //* 1. 从tenantDto 中获取机构，员工，套餐 数据
        Tenant tenant = tenantDto.getTenant();
        Employee employee = tenantDto.getEmployee();
        Long mealId = tenantDto.getMealId();
        
        //* 2. 判断数据的合法性（非空，重复，格式验证） // 一定不要相信前端
        JudgmentData(tenant, employee);

        //* 3. 保存机构数据
        Date date = new Date();
        savaTenant(tenant, date);

        //* 4. 保存员工数据
        saveEmp (tenant, employee, date);
        
        //* 5. 保存机构和套餐的关系
        saveMeal (tenant, mealId, date);

    }

    private void saveMeal(Tenant tenant, Long mealId, Date date) {
        Long tenantId = tenant.getId();
        Date expireDate = DateUtils.addDays(date, mealTime);
        Integer state = 1;

        baseMapper.saveTenantAndMeal(mealId, tenantId,expireDate, state);
    }

    private void saveEmp(Tenant tenant, Employee employee, Date date) {
        // 密码加密
        String salt = MD5.getRandomCode(18);
        String md5 = MD5.getMD5(employee.getPassword() + salt);
        employee.setPassword(md5);
        employee.setSalt(salt);
        employee.setState(Employee.NORMAL); //
        employee.setInputTime(date);
        employee.setTenantId(tenant.getId());
        employee.setType(Employee.PLATFORM_ADMIN);
        employeeMapper.insert(employee);
    }

    private void savaTenant(Tenant tenant, Date date) {
        // 注： 不要直接就保存前端接收到的数据, 自己设置默认值
        tenant.setRegisterTime(date);
        tenant.setState(Tenant.PENDING_REVIEW); // 0 魔鬼数字 -- 抽取常量
        baseMapper.insert(tenant);
    }

    private void JudgmentData(Tenant tenant, Employee employee) {
        //2.1 非空判断 公司名不可为空
       // ValidityUtils.isNotNull(tenant.getCompanyName(), ErrorCode.TENANT_NAME_NOTNULL.getMsg());

        //2.1 重复 公司名不可重复 -- 查询数据库
       // Tenant tenantForDB = baseMapper.selectOneByName(tenant.getCompanyName());
        
        // 不为空 则说明有对象 
       // ValidityUtils.isNull(tenantForDB,ErrorCode.TENANT_NAME_DOUBLE.getMsg());

        //2.3 格式验证 邮箱格式
       // ValidityUtils.isEmail(employee.getEmail(),ErrorCode.EMAIL_FORMAT_ERROR.getMsg());

        
    }
    
}
